Skip to content

Conversation

eugeneepshteyn
Copy link
Contributor

CDEFINED declarations are similar to "extern" declarations in C. If they have initializers, this could lead to linker errors. clang warns about "extern" declarations with initializers. Add similar warning to flang:

$ flang -c cdefined.f90 -pedantic
./cdefined.f90:3:57: warning: CDEFINED variable should not have an initializer [-Wcdefined-init]
    integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 4
                                                          ^

CDEFINED declarations are similar to "extern" declarations in C. If
they have initializers, this could lead to linker errors. clang warns
about "extern" declarations with initializers. Add similar warning
to flang:

```
$ flang -c cdefined.f90 -pedantic
./cdefined.f90:3:57: warning: CDEFINED variable should not have an initializer [-Wcdefined-init]
    integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 4
                                                          ^
```
@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review September 17, 2025 22:49
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

CDEFINED declarations are similar to "extern" declarations in C. If they have initializers, this could lead to linker errors. clang warns about "extern" declarations with initializers. Add similar warning to flang:

$ flang -c cdefined.f90 -pedantic
./cdefined.f90:3:57: warning: CDEFINED variable should not have an initializer [-Wcdefined-init]
    integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 4
                                                          ^

Full diff: https://github.com/llvm/llvm-project/pull/159456.diff

3 Files Affected:

  • (modified) flang/include/flang/Support/Fortran-features.h (+1-1)
  • (modified) flang/lib/Semantics/resolve-names.cpp (+3)
  • (added) flang/test/Semantics/cdefined.f90 (+6)
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 83a75b0efcb55..89d1dba7a2dc8 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -73,7 +73,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     ZeroDoStep, UnusedForallIndex, OpenMPUsage, DataLength, IgnoredDirective,
     HomonymousSpecific, HomonymousResult, IgnoredIntrinsicFunctionType,
     PreviousScalarUse, RedeclaredInaccessibleComponent, ImplicitShared,
-    IndexVarRedefinition, IncompatibleImplicitInterfaces,
+    IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
     VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
     MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
     CompatibleDeclarationsFromDistinctModules,
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index d0d3b0e1caa5a..cdd8d6ff2f60e 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9118,6 +9118,9 @@ void DeclarationVisitor::NonPointerInitialization(
         if (details->init()) {
           SayWithDecl(name, *name.symbol,
               "'%s' has already been initialized"_err_en_US);
+        } else if (details->isCDefined()) {
+          context().Warn(common::UsageWarning::CdefinedInit, name.source,
+              "CDEFINED variable should not have an initializer"_warn_en_US);
         } else if (IsAllocatable(ultimate)) {
           Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
         } else if (ultimate.owner().IsParameterizedDerivedType()) {
diff --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
new file mode 100644
index 0000000000000..84103ce661d0b
--- /dev/null
+++ b/flang/test/Semantics/cdefined.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+module m
+  use iso_c_binding
+  !WARNING: CDEFINED variable should not have an initializer [-Wcdefined-init]
+  integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
+end

@eugeneepshteyn eugeneepshteyn merged commit 91c72e8 into llvm:main Sep 18, 2025
14 checks passed
@eugeneepshteyn eugeneepshteyn deleted the cdefined-init-warning branch September 18, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants